package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;

public class _10yanNewsAnalyse implements AnalyseNews {

	//http://www.10yan.com/2013/0507/37960.shtml
	private static final String[] regex={
		"http://www.10yan.com/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://.*.10yan.com/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://v.10yan.com/html/.*/[0-9]{4,}.Html",
		"http://12345.10yan.com/html/.*/[0-9]{4,}.html",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		title=doc.select("td[style=FONT-SIZE:24px;LINE-HEIGHT:120%;LETTER-SPACING:1px;font-weight:bold;color:#1f3a87;]").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("#video_3j_l_title").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}
		content =HtmlCleaner.getContentHtml(url, doc.select("div.article-content"));
		if (StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select(".cont-detail"));
		}
		if (StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select("#video_3j_l_info"));
		}
		if (StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select(".summary"));
		}
		if (StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select(".info_content"));
		}

		author=JSoupUtils.matchAuthor(doc, "来源：");
		if (StringUtils.isBlank(author)){
			author = doc.select(".source").text();
		}
		date=DateUtils.matchDate(doc.select("td[height=30][align=center]").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("span.post-time").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("#video_3j_l_xinxi").text());
		}
		if(date == null){
			date= JSoupUtils.matchDate(doc, "日期：","来源:");
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		String url="http://12345.10yan.com/html/rxtb/3025.html";
		_10yanNewsAnalyse test=new _10yanNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(test.isDetailPage(url));
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
